import * as API_LIST from '../apis/setting';
import * as API from "../apis/moduleSetting";
import {observable, action, toJS} from 'mobx';
import {message} from 'antd';
import {WeaTableNew, WeaForm} from 'comsMobx';
const {TableStore} = WeaTableNew;
import {WeaLocaleProvider} from "ecCom";
const getLabel = WeaLocaleProvider.getLabel;

class Setting {
    @observable title = getLabel(26630, "微博设置");
    @observable tabDatas = [
        {title: getLabel(24128, "基本设置"), key: "0"},
        {title: getLabel(26945, "分享设置"), key: "1"},
        {title: getLabel(16448, "模板设置"), key: "2"},
    ];
    @observable tabkey = 0;

    //基本设置
    @observable baseStatus = {
        checked: 0,
        ifAllowRequest: 1,
        baseSaveLoading: false,
    }

    //模版设置
    @observable moduleForm = new WeaForm(); //高级搜索
    @observable moduleTableStore = new TableStore();
    @observable moduleDialogForm = new WeaForm(); //新建、编辑基本信息
    @observable moduleShareTable = new TableStore(); //共享范围
    @observable moduleStatus = {
        selectedKey: 0,
        showSearchAd: false,
        moduleFields: [],
        moduleSaveLoading: false,
        defaultBtId: '',
        moduleDialogFields: [],
        //新建
        showCreate: false, //新建
        createLoading: false,

        //编辑
        showEdit: false, //编辑
        editBaseLoading: false,
        editContentLoading: false,
        editId: '',
        richTextValue: '',

        //预览
        showPreview: false, //预览

        //添加共享范围
        showShare: false,
        shareList: [],
    }





    @observable
    Card ={}

    getCardStorefun = (store)=>{
        this.Card = store;
    }


    @observable
    cubeLayoutParame = {};

    @observable
    partid = 0;


    @observable
    templateType = "0" ;

    @observable
    isEcme = "0";

    @action

    doInit = () => {
        this.tabkey = 0;
        this.moduleForm = new WeaForm();
        this.moduleTableStore = new TableStore();
        this.getUserBlogBaseSetting();
        this.getBlogDefaultTemplate(true);//获得选中的默认id,之后再渲染table
        this.getBlogTemplateSearchCondition();
        this.getBlogTemplateAddCondition();
        this.getBlogTemplateShareCondition();
        this.getBlogTemplateShareAddCondition();
    }

    doEditInit = (tempid) => {//编辑接口初始化
        this.getBlogTemplateShareCondition({tempid: tempid || this.moduleStatus.editId});
        this.getBlogTemplateContent({tempid: tempid || this.moduleStatus.editId});
        this.getBlogTemplateShareAddCondition();
    }

    setModuleState = (params = {}) => {
        let moduleStatus = {...this.moduleStatus};
        Object.keys(params).forEach(key => {
            moduleStatus[key] = params[key];
        });
        this.moduleStatus = moduleStatus;
    }

    doModuleFormInit = (condition) => {
        this.moduleDialogForm = new WeaForm();
        this.moduleDialogForm.initFormFields(condition || toJS(this.moduleStatus.moduleDialogFields));
        window.e9ChangedFormFieldKey = 'hjwadjsgbkkk';
    }

    //微博基本设置- 获取
    getUserBlogBaseSetting = (value = {}) => {
        API_LIST.getUserBlogBaseSettingP(value).then(action(data => {
            this.baseStatus.checked = data && data.userBlogBaseSetting && data.userBlogBaseSetting.isReceive;
            this.baseStatus.ifAllowRequest = data.allowRequest;
        }));
    }

    //微博基本设置-保存
    saveBlogUserSetting = (value = {}) => {
        this.baseStatus.baseSaveLoading = true;
        API_LIST.saveBlogUserSettingP(value).then(result => {
            this.baseStatus.baseSaveLoading = false;
            if(result.api_status == 'true'){
                message.success(`${getLabel(18758,'保存成功')}`);
            }else {
                message.error(`${getLabel(22620,'保存失败')}`);
            }
        });
    }

    getBlogDefaultTemplate = (isOnChange = false) => {
        API.getBlogDefaultTemplate().then(result => {
            this.moduleStatus.defaultBtId = result.defaultBtId;
            this.getBlogTemplateListCondition(isOnChange);
        })
    }

    //模板列表
    getBlogTemplateListCondition = (isOnChange = false) => {
        const params = this.moduleForm && this.moduleForm.getFormParams() || {};
        API.getBlogTemplateListCondition(params).then(result => {
            isOnChange ? this.moduleTableStore.getDatas(result.sessionkey, 1) : this.moduleTableStore.getDatas(result.sessionkey);
        })
    }

    //高级搜索
    getBlogTemplateSearchCondition = () => {
        API.getBlogTemplateSearchCondition().then(result => {
            this.moduleForm = new WeaForm();
            this.moduleForm.initFormFields(result.conditioninfo);
            this.moduleStatus.moduleFields = result.conditioninfo;
        })
    }

    //新建模板
    getBlogTemplateAddCondition = (params) => {
        if(typeof params === 'object') {
            params = Object.assign(params,{
                is_multilang_set:true
            });
        }else{
            params={
                is_multilang_set:true
            }
        }
        API.getBlogTemplateAddCondition(params).then(result => {
            this.templateType=result.templateType;
            this.doModuleFormInit(result.conditioninfo);
            this.moduleStatus.moduleDialogFields = result.conditioninfo;
            this.isEcme = result.isEcme;
        })
    }

    // 保存默认模版
    setUserBlogTemplateToDefault = (params) => {
        API.setUserBlogTemplateToDefault(params).then(result => {
            if(result.status == 1){
                message.success(getLabel(83885, '操作成功'));
            }else {
                message.error(getLabel(4715, '操作失败'));
            }
        })
    }

    //模板操作
    operateBlogTemplateSet = (params, type) => {
        this.templateType=params.templateType;
        if(type == 'create'){this.moduleStatus.createLoading = true;}
        if(type == 'editBase'){this.moduleStatus.editBaseLoading = true;}
        if(type == 'editContent'){this.moduleStatus.editContentLoading = true;}
        API.operateBlogTemplateSet(params).then(result => {
            this.moduleStatus = {...this.moduleStatus, createLoading: false, editBaseLoading: false, editContentLoading: false, richTextValue: ''};
            if(result.status == 1){
                if(type == 'create'){//保存
                    this.moduleStatus.showCreate = false;
                    message.success(`${getLabel(83885, '操作成功')}`);
                    this.doModuleFormInit();
                }
                if(type == 'createSet'){//保存进入详细设置
                    this.doEditInit(result.tempId);
                    this.moduleStatus = {
                        ...this.moduleStatus,
                        editId: result.tempId,
                        showCreate: false,
                        showEdit: true
                    }
                }
                if(type == 'editBase' || type == 'editContent'){//编辑
                    this.moduleStatus.showEdit = false;
                    message.success(`${getLabel(83885, '操作成功')}`);
                    this.doModuleFormInit();
                }
                this.getBlogDefaultTemplate();
            }else {
                message.success(`${getLabel(4715, '操作失败')}`);
            }
        })
    }

    //模板分享列表
    getBlogTemplateShareCondition = (params) => {
        API.getBlogTemplateShareCondition(params).then(result => {
            this.moduleShareTable.getDatas(result.sessionkey);
        })
    }

    //获取模板内容
    getBlogTemplateContent = (params) => {
        API.getBlogTemplateContent(params).then(result => {


            if(result.templateType=='1'){
                this.cubeLayoutParame= result.data;
                this.templateType = '1';
                this.partid = result.partid;
            }else{
               this.cubeLayoutParame = {};
                this.partid  = 0;
            }
            this.moduleStatus.richTextValue = result.tempContent;
        })
    }

    //模板分享添加
    getBlogTemplateShareAddCondition = () => {
        API.getBlogTemplateShareAddCondition().then(result => {
            this.moduleStatus.shareList = result.conditioninfo;
        })
    }

    //模板分享操作
    operateBlogTemplateShare = (params) => {
        API.operateBlogTemplateShare(params).then(result => {
            if (result.status == 1) {
                this.moduleStatus.showShare = false;
                this.getBlogTemplateShareCondition({tempid: this.moduleStatus.editId});
            } else {
                message.error(getLabel(4715, '操作失败'));
            }
        })
    }

    //切换tab
    changeTab = value => {
        this.tabkey = value;
    }

    //切换switch状态
    changeChecked = value => {
        this.baseStatus.checked = value;
    }

}

const blogSetting = new Setting();
export default blogSetting;